package com.ruoyi.business.service.impl;

import java.util.List;

import com.ruoyi.business.constant.JtUserConstants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.business.mapper.JtAppUserMapper;
import com.ruoyi.business.domain.JtAppUser;
import com.ruoyi.business.service.IJtAppUserService;
import com.ruoyi.common.core.text.Convert;

/**
 * APP用户Service业务层处理
 * 
 * @author Alan Zhang
 * @date 2025-03-31
 */
@Service
public class JtAppUserServiceImpl implements IJtAppUserService 
{
    @Autowired
    private JtAppUserMapper jtAppUserMapper;

    /**
     * 查询APP用户
     * 
     * @param userId APP用户主键
     * @return APP用户
     */
    @Override
    public JtAppUser selectJtAppUserByUserId(Long userId)
    {
        return jtAppUserMapper.selectJtAppUserByUserId(userId);
    }

    /**
     * 查询APP用户列表
     * 
     * @param jtAppUser APP用户
     * @return APP用户
     */
    @Override
    public List<JtAppUser> selectJtAppUserList(JtAppUser jtAppUser)
    {
        return jtAppUserMapper.selectJtAppUserList(jtAppUser);
    }

    /**
     * 新增APP用户
     * 
     * @param jtAppUser APP用户
     * @return 结果
     */
    @Override
    public int insertJtAppUser(JtAppUser jtAppUser)
    {
        jtAppUser.setCreateTime(DateUtils.getNowDate());
        return jtAppUserMapper.insertJtAppUser(jtAppUser);
    }

    /**
     * 修改APP用户
     * 
     * @param jtAppUser APP用户
     * @return 结果
     */
    @Override
    public int updateJtAppUser(JtAppUser jtAppUser)
    {
        jtAppUser.setUpdateTime(DateUtils.getNowDate());
        return jtAppUserMapper.updateJtAppUser(jtAppUser);
    }

    /**
     * 批量删除APP用户
     * 
     * @param userIds 需要删除的APP用户主键
     * @return 结果
     */
    @Override
    public int deleteJtAppUserByUserIds(String userIds)
    {
        return jtAppUserMapper.deleteJtAppUserByUserIds(Convert.toStrArray(userIds));
    }

    /**
     * 删除APP用户信息
     * 
     * @param userId APP用户主键
     * @return 结果
     */
    @Override
    public int deleteJtAppUserByUserId(Long userId)
    {
        return jtAppUserMapper.deleteJtAppUserByUserId(userId);
    }


    /**
     * 通过手机号码查询用户
     *
     * @param phoneNumber 手机号码
     * @return 用户对象信息
     */
    @Override
    public JtAppUser selectUserByPhoneNumber(String phoneNumber){
        return jtAppUserMapper.selectUserByPhoneNumber(phoneNumber);
    }

    /**
     * 通过邮箱查询用户
     *
     * @param email 邮箱
     * @return 用户对象信息
     */
    @Override
    public JtAppUser selectUserByEmail(String email){
        return jtAppUserMapper.selectUserByEmail(email);
    }

    /**
     * 校验手机号码是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public boolean checkPhoneUnique(JtAppUser user)
    {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        JtAppUser appUser = jtAppUserMapper.checkPhoneUnique(user.getPhoneNumber());
        if (StringUtils.isNotNull(appUser) && appUser.getUserId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验email是否唯一
     *
     * @param user 用户信息
     * @return
     */
    @Override
    public boolean checkEmailUnique(JtAppUser user)
    {
        Long userId = StringUtils.isNull(user.getUserId()) ? -1L : user.getUserId();
        JtAppUser appUser = jtAppUserMapper.checkEmailUnique(user.getEmail());
        if (StringUtils.isNotNull(appUser) && appUser.getUserId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 校验手机号码是否唯一
     *
     * @param phoneNumber 用户手机
     * @return
     */
    @Override
    public JtAppUser checkPhoneUnique(String phoneNumber)
    {
        JtAppUser appUser = jtAppUserMapper.checkPhoneUnique(phoneNumber);
        return appUser;
    }

    /**
     * 校验email是否唯一
     *
     * @param email 用户邮箱
     * @return
     */
    @Override
    public JtAppUser checkEmailUnique(String email)
    {
        JtAppUser appUser = jtAppUserMapper.checkEmailUnique(email);
        return appUser;
    }


    /**
     * 注册用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public boolean registerUser(JtAppUser user)
    {
        user.setUserType(JtUserConstants.APP_USER_TYPE);
        user.setApplyStatus(JtUserConstants.USER_APPLY_STATUS);
        return jtAppUserMapper.insertJtAppUser(user) > 0;
    }

    /**
     * 修改用户密码
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int resetUserPwd(JtAppUser user)
    {
        return updateJtAppUser(user);
    }


    /**
     * 统计 用户数
     * @return
     */
    @Override
    public Long selectAppUserCount(String beginCreateTime, String endCreateTime) {
        return jtAppUserMapper.selectAppUserCount(beginCreateTime, endCreateTime);
    }
}
